using global::GoGameClient.script.global;
using Godot;
using GoGameClient.script.cmd;
using GoGameClient.script.dialog;
using GoGameClient.script.network.webSocket;
using GoGameClient.script.tool;

namespace GoGameClient.script.node.caption;

public partial class LoginGui : Control
{
    // Called when the node enters the scene tree for the first time.
    private LineEdit _account;
    private LineEdit _password;
    private CheckBox _autoLogin;
    private string _scenePath = "res://scene/SelectRole.tscn";

    public override void _Ready()
    {
        _account = GetNode<LineEdit>("Account");
        _password = GetNode<LineEdit>("Password");
        _autoLogin = GetNode<CheckBox>("AutoLogin");
        ResourceLoader.LoadThreadedRequest(_scenePath);
    }

    private void LoginButtonPressed()
    {
        var account = _account.Text;
        var password = _password.Text;
        if (account == null || password == null || account.Length == 0 || password.Length == 0)
        {
            TipsDialogUtil.ShowTips(this, "请输入账户和密码");
            return;
        }

        SocketControl.Instance().Request(new BodyRequestConfig<LoginVerifyReq, UserRes>
        {
            Title = "用户登录",
            Cmd = (int)Router.Login,
            CmdMethod = (int)LoginRouter.LoginVerify,
            Data = new LoginVerifyReq
            {
                Account = account,
                Password = password
            },
            Result = userRes =>
            {
                UserControl.GetInstance().SetUserId(userRes.Id);
                TipsDialogUtil.ShowTips(this, "登录成功");
                if (ResourceLoader.LoadThreadedGetStatus(_scenePath) != ResourceLoader.ThreadLoadStatus.Loaded)
                {
                    return;
                }

                Config.SetLogin(true);
                GetTree().ChangeSceneToPacked((PackedScene)ResourceLoader.LoadThreadedGet(_scenePath));
            }
        });
    }


    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
    }
}